<?php
namespace Admin\Model;
use Think\Model;
class UserModel extends Model {
  protected $_validate = array(
    //用户添加修改的正则表达式 
    array('username','require','用户名不能为空',0,'regex',3),
    array('password','require','密码不能为空',0,'regex',3),
    array('repassword','password','两次密码不一致',0,'confirm',3),
    //用户登录的正则表达式
    array('username','require','用户名不能为空',0,'regex',4),
    array('password','require','密码不能为空',0,'regex',4),

  ); 

  // 插入数据前的回调方法
  protected function _before_insert(&$data,$options) {
    $data['password'] = md5(md5($data['password'].C('salt')));
    $data['add_time'] = time();
  }

  // 插入数据前的回调方法
  protected function _before_update(&$data,$options) {
    $data['password'] = md5(md5($data['password'].C('salt')));
    $data['add_time'] = time();
  }

  //封装登录chckLogin的方法
  public function checkLogin($username,$password){
    $where=array(
      'username' => $username,
      'password' => md5(md5($password.C('salt'))),
    );
    $userInfo = $this->where($where)->find();
    if($userInfo){
      session('username',$userInfo['username']);
      session('user_id',$userInfo['user_id']);
      return true;
    }else{
      return false;
    }
  }

}